package pl.edu.agh.ki.ewywiadowka.client.windows.secretary;

import java.util.Date;
import java.util.LinkedList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;

import pl.edu.agh.ki.ewywiadowka.client.Const;
import pl.edu.agh.ki.ewywiadowka.client.EWywiadowka;
import pl.edu.agh.ki.ewywiadowka.client.dto.ClassDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.TeacherDTO;
import pl.edu.agh.ki.ewywiadowka.client.windows.GenericWindow;

public class RegisterClassWindow extends GenericWindow implements ClickHandler, KeyUpHandler {

	private VerticalPanel _mainPanel = new VerticalPanel();
	
	private Grid _formular;
	public ListBox _annualDateBox = new ListBox();
	public TextBox _letterTextBox = new TextBox();
	public ListBox _educatorListBox = new ListBox(false);
	
	public Button _okButton = new Button("Dodaj klase");
	
	public LinkedList<TeacherDTO> _teachers = null;
	
	
	public RegisterClassWindow(EWywiadowka wywiadowka) {
		super(wywiadowka);
		
		for (int year = new Date().getYear() + 1900; year > 1980; year--)
			_annualDateBox.addItem(String.valueOf(year));
		
		_annualDateBox.setSelectedIndex(0);
		_annualDateBox.setVisibleItemCount(5);
		_letterTextBox.setText("a");
		
		_formular = new Grid(3, 2);
		_formular.setWidget(0, 0, new Label("Rocznik"));
		_formular.setWidget(0, 1, _annualDateBox);
		_formular.setWidget(1, 0, new Label("Litera"));
		_formular.setWidget(1, 1, _letterTextBox);
		_formular.setWidget(2, 0, new Label("Wychowawca"));
		_formular.setWidget(2, 1, _educatorListBox);
		
		Const.eWywiadowkaService.getPotentialEducators(new AsyncCallback<LinkedList<TeacherDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(LinkedList<TeacherDTO> result) {
				
				_teachers = result;
				
				for (TeacherDTO educator : result) {
					String item = educator.name + educator.surname;
					_educatorListBox.addItem(item);
				}
				
			}
			
		});
		
		
		_letterTextBox.addKeyUpHandler(this);
		_okButton.addClickHandler(this);
		
		_mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		_mainPanel.add(_formular);
		_mainPanel.add(_okButton);
		
		this.initWidget(_mainPanel);
		
	}
	
	
	
	void classRegister() {
		if (_letterTextBox.getText().length() == 1) {
			Character letter = _letterTextBox.getText().toLowerCase().toCharArray()[0];
			Date date = new Date();
			date.setYear(Integer.valueOf(_annualDateBox.getValue(_annualDateBox.getSelectedIndex())) - 1900);
			date.setMonth(0);
			date.setDate(0);
			ClassDTO clas = new ClassDTO(null, date, letter, _teachers.get(_educatorListBox.getSelectedIndex()));
			
			Const.eWywiadowkaService.registerClass(clas, new AsyncCallback<Const.Error.ClassRegisterMsg>(){

				@Override
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage());
				}
				
				@Override
				public void onSuccess(Const.Error.ClassRegisterMsg result) {
					Window.alert(result.toString());
				}
				
			});
			
		}
		else 
			Window.alert("Prosze podac litere klasy");
	}

	@Override
	public void onClick(ClickEvent event) {
		classRegister();
	}

	@Override
	public void onKeyUp(KeyUpEvent event) {
		if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
			classRegister();
		}
		
	}

}
